package com.company.project.service.impl;

import com.company.project.dao.PeriodicalsExcellentMapper;
import com.company.project.dao.PeriodicalsMapper;
import com.company.project.dao.ThesisAssessmentResultMapper;
import com.company.project.model.*;
import com.company.project.model.Dictionary;
import com.company.project.service.*;
import com.company.project.core.AbstractService;
import com.company.project.util.AdminPAndPCRAndDBRAndPE;
import com.company.project.util.PageUtil;
import com.company.project.util.periodicalsWhere;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class PeriodicalsServiceImpl extends AbstractService<Periodicals> implements PeriodicalsService {
    @Resource
    @Autowired
    private PeriodicalsMapper periodicalsMapper;

    @Resource
    private PeriodicalsDbRService periodicalsDbRService;

    @Resource
    private PeriodicalsCategoryRService periodicalsCategoryRService;

    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private ThesisAssessmentResultMapper thesisAssessmentResultMapper;

    @Resource
    @Autowired
    private PeriodicalsExcellentService periodicalsExcellentService;

    @Override
    public PageUtil<Map<Object,Object>> queryPM(String cname,Integer index) {
        HashMap<Object,Object> map1 = new HashMap();
        map1.put("value","期刊");
        PageUtil<Map<Object,Object>> pageUtil = new PageUtil();
        pageUtil.setPageSize(15);
        pageUtil.setCpage(index);
        pageUtil.setTotalRow(this.queryCount(cname));
        List<Map<Object,Object>> maps = periodicalsMapper.queryPM(pageUtil, cname);
        for (int i=0;i<maps.size();i++){
            maps.get(i).putAll(map1);
        }
        pageUtil.setLists(maps);
        return pageUtil;
    }

    @Override
    public Integer queryCount(String cname) {
        return periodicalsMapper.queryCount(cname);
    }

    @Override
    public PageUtil<Periodicals> getPeriodicalsByWhere(periodicalsWhere pw, Integer index) {
        if (pw == null) {
            pw = new periodicalsWhere();
        }
        PageUtil<Periodicals> pageUtil = new PageUtil();
        pageUtil.setPageSize(6);
        pageUtil.setCpage(index);
        pageUtil.setTotalRow(this.getCount(pw));
        pw.setPageUtil(pageUtil);
        pageUtil.setLists(periodicalsMapper.selectPeriodicalsByWhere(pw));
        return pageUtil;
    }

    @Override
    public Integer getCount(periodicalsWhere pw) {
        return periodicalsMapper.selectCounts(pw);
    }

    @Override
    public Periodicals selectPeriodicalsById(String id) {
        return periodicalsMapper.selectPeriodicalsById(id);
    }

    //获取全部优秀期刊，分页
    @Override
    public PageUtil<Periodicals> selectPeriodicalsExcellence(Integer pageIndex) {
        PageUtil<Periodicals> pageUtil = new PageUtil();
        pageUtil.setPageSize(5);
        pageUtil.setCpage(pageIndex);
        pageUtil.setTotalRow(periodicalsExcellentService.getAllCount());
        pageUtil.setLists(periodicalsMapper.selectPeriodicalsExcellence(pageUtil));
        return pageUtil;
    }

    public List<Periodicals> getPeriodicalsExcellenceImg() {
        return periodicalsMapper.selectPeriodicalsExcellenceImg();
    }

    @Override
    public String getPeriodicalsName(Integer id) {
        return periodicalsMapper.SelectPeriodicalsName(id);
    }

    //后台查询期刊
    @Override
    public Map<String, Object> adminGetPeriodicalsList(Integer pageIndex) {
        Map<String, Object> result = new HashMap<>();
        List<Periodicals> periodicals = new ArrayList<>();
        List<Periodicals> periodicalsExcellent = new ArrayList<>();
        PageUtil<Periodicals> pageUtil = new PageUtil<>();
        pageUtil.setPageSize(15);
        pageUtil.setCpage(pageIndex);
        pageUtil.setTotalRow(this.adminGetPeriodicalsCount());
        List<Periodicals> periodicalsList = periodicalsMapper.adminSelectPeriodicalsList(pageUtil);
        for (Periodicals p : periodicalsList) {
            if (p.getPeid() != null) {
                periodicalsExcellent.add(p);
            } else {
                p.setPeid(0);
                periodicals.add(p);
            }
        }
        result.put("periodicals", periodicals);
        result.put("periodicalsExcellent", periodicalsExcellent);
        result.put("totalPage", pageUtil.getTotalPage());

        return result;
    }


    @Override
    //后台查询单个期刊
    public Periodicals adminGettPeriodicals(Integer id) {
        Periodicals s = periodicalsMapper.adminSelectPeriodicals(id);
        int i = 0;
        return s;
    }

    @Override
    public Integer adminGetPeriodicalsCount() {
        return periodicalsMapper.adminSelectPeriodicalsCount();
    }

    //后台添加期刊
    @Override
    public boolean adminAddPeriodicals(AdminPAndPCRAndDBRAndPE pde) {
        Date date = new Date();
        Integer p = 0;
        Integer pdbr = 0;
        Integer pcr = 0;
        Integer pe = 0;
        if (pde.getPeriodicals() != null) {

            pde.getPeriodicals().setCreateTime(date);
            pde.getPeriodicals().setLastModifyTime(date);
            pde.getPeriodicals().setLastModifyUser(pde.getPeriodicals().getCreateUser());
            p = periodicalsMapper.insertSelective(pde.getPeriodicals());
            if(p==0){
                return false;
            }
        }
        if (pde.getDbid().size() != 0) {
            List<PeriodicalsDbR> periodicalsDbRList = new ArrayList<>();
            for (Integer i = 0; i< pde.getDbid().size(); i++){
                PeriodicalsDbR periodicalsDbR = new PeriodicalsDbR();
                periodicalsDbR.setCreateUser(pde.getPeriodicals().getCreateUser());
                periodicalsDbR.setLastModifyUser(pde.getPeriodicals().getCreateUser());
                periodicalsDbR.setPid(pde.getPeriodicals().getId());
                periodicalsDbR.setDbid(pde.getDbid().get(i));
                periodicalsDbR.setCreateTime(date);
                periodicalsDbR.setLastModifyTime(date);
                periodicalsDbRList.add(periodicalsDbR);
            }
            pdbr=periodicalsDbRService.adminAddPeriodicalsDbR(periodicalsDbRList);
            if(pdbr==0){
                return false;
            }
        }
        if (pde.getCid().size() != 0) {
            List<PeriodicalsCategoryR> periodicalsCategoryRList = new ArrayList<>();
            for (int i = 0; i<pde.getCid().size(); i++){
                PeriodicalsCategoryR periodicalsCategoryR = new PeriodicalsCategoryR();
                periodicalsCategoryR.setPid(pde.getPeriodicals().getId());
                periodicalsCategoryR.setCreateUser(pde.getPeriodicals().getCreateUser());
                periodicalsCategoryR.setLastModifyUser(pde.getPeriodicals().getCreateUser());
                periodicalsCategoryR.setCreateTime(date);
                periodicalsCategoryR.setLastModifyTime(date);
                periodicalsCategoryR.setCid(pde.getCid().get(i));
                periodicalsCategoryRList.add(periodicalsCategoryR);
            }
            pcr = periodicalsCategoryRService.adminAddPeriodicalsCategoryR(periodicalsCategoryRList);
            if(pcr==0){
                return false;
            }
        }
        if(pde.getPeriodicals().getPeid()!=null){
            if(pde.getPeriodicals().getPeid() == 1 ){
                PeriodicalsExcellent e = new PeriodicalsExcellent();
                e.setPid(pde.getPeriodicals().getId());
                e.setCreateTime(date);
                e.setCreateUser(pde.getPeriodicals().getCreateUser());
                e.setLastModifyTime(date);
                e.setLastModifyUser(pde.getPeriodicals().getCreateUser());
                pe=periodicalsExcellentService.adminAddPeriodicalsExcellent(e);
                if(pe==0){
                    return false;
                }
            }
        }
        return true;

    }

    //后台修改期刊
    public boolean adminUpdatePeriodicals(AdminPAndPCRAndDBRAndPE pde){
        Date date = new Date();
        Integer p = 0;
        Integer pdbr = 0;
        Integer dpdbr = 0;
        Integer pcr = 0;
        Integer dpcr = 0;
        Integer pe = 0;
        Integer dpe = 0;
        if (pde.getPeriodicals() != null) {
            pde.getPeriodicals().setLastModifyTime(date);
            p = periodicalsMapper.updateByPrimaryKeySelective(pde.getPeriodicals());
            if(p==0){
                return false;
            }
        }
        if (pde.getDbid().size() != 0) {
            List<PeriodicalsDbR> periodicalsDbRList = new ArrayList<>();
            for (Integer i = 0; i< pde.getDbid().size(); i++){
                PeriodicalsDbR periodicalsDbR = new PeriodicalsDbR();
                periodicalsDbR.setCreateUser(pde.getPeriodicals().getCreateUser());
                periodicalsDbR.setLastModifyUser(pde.getPeriodicals().getCreateUser());
                periodicalsDbR.setPid(pde.getPeriodicals().getId());
                periodicalsDbR.setCreateTime(date);
                periodicalsDbR.setLastModifyTime(date);
                periodicalsDbR.setDbid(pde.getDbid().get(i));
                periodicalsDbRList.add(periodicalsDbR);
            }
            dpdbr=periodicalsDbRService.adminDelectByPid(pde.getPeriodicals().getId());
            pdbr=periodicalsDbRService.adminAddPeriodicalsDbR(periodicalsDbRList);
            if(pdbr==0){
                return false;
            }
        }
        if (pde.getCid().size() != 0) {
            List<PeriodicalsCategoryR> periodicalsCategoryRList = new ArrayList<>();
            for (int i = 0; i<pde.getCid().size(); i++){
                PeriodicalsCategoryR periodicalsCategoryR = new PeriodicalsCategoryR();
                periodicalsCategoryR.setCreateUser(pde.getPeriodicals().getCreateUser());
                periodicalsCategoryR.setLastModifyUser(pde.getPeriodicals().getCreateUser());
                periodicalsCategoryR.setCreateTime(date);
                periodicalsCategoryR.setLastModifyTime(date);
                periodicalsCategoryR.setPid(pde.getPeriodicals().getId());
                periodicalsCategoryR.setCid(pde.getCid().get(i));
                periodicalsCategoryRList.add(periodicalsCategoryR);
            }
            dpcr=periodicalsCategoryRService.adminDelectByPid(pde.getPeriodicals().getId());
            pcr = periodicalsCategoryRService.adminAddPeriodicalsCategoryR(periodicalsCategoryRList);
            if(pcr==0){
                return false;
            }
        }
        if(pde.getPeriodicals().getPeid()==1){
            dpe = periodicalsExcellentService.adminDelectByPid(pde.getPeriodicals().getId());
            PeriodicalsExcellent periodicalsExcellent = new PeriodicalsExcellent();
            periodicalsExcellent.setCreateTime(date);
            periodicalsExcellent.setCreateUser(pde.getPeriodicals().getCreateUser());
            periodicalsExcellent.setLastModifyTime(date);
            periodicalsExcellent.setLastModifyUser(pde.getPeriodicals().getCreateUser());
            periodicalsExcellent.setPid(pde.getPeriodicals().getId());
            pe=periodicalsExcellentService.adminAddPeriodicalsExcellent(periodicalsExcellent);
            if(pe==0){
                return false;
            }
        }else {
            dpe = periodicalsExcellentService.adminDelectByPid(pde.getPeriodicals().getId());
        }



        return  true;
    }

    //期刊删除
    @Override
    public boolean adminDelectPeriodicals(Integer id) {
        int pdb = 0;
        int pcr = 0;
        int p = 0;
        /*Integer integer = thesisAssessmentResultMapper.selectTaid(id);
        if (integer>0){
            return false;
        }*/
        pdb=periodicalsDbRService.adminDelectByPid(id);
        pcr=periodicalsCategoryRService.adminDelectByPid(id);
        p=periodicalsMapper.adminDelectPeriodicals(id);
        periodicalsExcellentService.adminDelectByPid(id);
        if(pdb==0 && pcr==0 && p==0){
            return false;
        }
        return true;
    }

    @Override
    public List<Periodicals> getPeriodicals(List<String> pnameList) {
        return periodicalsMapper.selectPeriodicals(pnameList);
    }

    //后台修改期刊状态
    @Override
    public boolean adminUpdateStatus(Periodicals periodicals) {
        int i =0;
        i= periodicalsMapper.updateStatus(periodicals);
        if(i!=0){
            return true;
        }
        return false;

    }


    //后台修改期刊
    /*@Override
    public boolean adminUpdatePeriodicals(AdminPAndPCRAndDBRAndPE pde) {
        Date date = new Date();
        Integer p = 0;
        Integer pe = 0;
        if (pde.getPeriodicals() != null) {
            pde.getPeriodicals().setLastModifyTime(date);
            p = periodicalsMapper.updateByPrimaryKeySelective(pde.getPeriodicals());
            if(p==0){
                return false;
            }
        }
        if (pde.getPeriodicalsDbR().size() != 0) {
            for (PeriodicalsDbR pdbrs : pde.getPeriodicalsDbR()) {
                Integer pdbr = 0;
                pdbrs.setLastModifyUser(pde.getPeriodicals().getLastModifyUser());
                pdbrs.setPid(pde.getPeriodicals().getId());
                pdbrs.setLastModifyTime(date);
                pdbr = periodicalsDbRService.adminUpdatePeriodicalsDbR(pdbrs);
                if (pdbr == 0) {
                    return false;
                }
            }
        }
        if (pde.getPeriodicalsCategoryR().size() != 0) {
            for (PeriodicalsCategoryR pcrs : pde.getPeriodicalsCategoryR()) {
                Integer pcr = 0;
                pcrs.setLastModifyUser(pde.getPeriodicals().getLastModifyUser());
                pcrs.setPid(pde.getPeriodicals().getId());
                pcrs.setLastModifyTime(date);
                pcr = periodicalsCategoryRService.adminUpdatePeriodicalsCategoryR(pcrs);
                if (pcr == 0) {
                    return false;
                }
            }
        }
        if(pde.getPeriodicalsExcellent()!=null){
            pde.getPeriodicalsExcellent().setLastModifyTime(date);
            pde.getPeriodicalsExcellent().setLastModifyUser(pde.getPeriodicals().getLastModifyUser());
            pde.getPeriodicalsExcellent().setPid(pde.getPeriodicals().getId());
            pe=periodicalsExcellentService.adminAddPeriodicalsExcellent(pde.getPeriodicalsExcellent());
            if(pe==0){
                return false;
            }
        }
        return true;
    }*/
}
